package com.tarena.luban.worker.server.controller;

import com.alibaba.fastjson.JSON;
import com.tarena.luban.worker.server.assemble.WorkerAssemble;
import com.tarena.luban.worker.server.bo.WorkerBO;
import com.tarena.luban.worker.server.service.WorkerServerService;
import com.tarena.luban.worker.server.vo.WorkerVO;
import com.tarena.passport.protocol.LoginUser;
import com.tarena.tp.attach.server.client.AttachApi;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import com.tedu.inn.protocol.exception.BusinessException;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Headers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class WorkerServerController {
    //注入一个domain workerServerService
    @Autowired
    private WorkerServerService workerServerService;
    @Autowired
    private WorkerAssemble workerAssemble;
    @PostMapping("/worker/create")
    public Long createWorker(
            @RequestBody WorkerCreateParam workerCreateParam,
            HttpServletRequest request) throws BusinessException {
        String authorization = request.getHeader("Authorization");
        System.out.println(authorization);
        //新增 让业务层返回一个新增的id
        return workerServerService.create(workerCreateParam);
    }
    /**
     * 查询师傅的详情
     * 前提是先入驻
     */
    @GetMapping("/worker/detail")
    public WorkerVO detailWorker() throws BusinessException {
        //controller需要封装的是一个VO对象 能指望service返回么
        //service 层返回的是BO对象 需要在adapter层 做装配(转化)assemble
        WorkerBO workerBO=workerServerService.detail();
        //装配成 VO assembler bo2vo bos2vos
        //装配的代码不是我们一行一行编写,所以需要注意是否因为BO对象的
        //封装数据问题,导致装配出现空指针
        if (workerBO == null) {
            return null;
        }
        WorkerVO workerVO = workerAssemble.assembleBO2VO(workerBO);
        return workerVO;
    }
}
